package com.sxyjhh.dbutil.utils;

import com.sxyjhh.dbutil.basic.*;
import com.sxyjhh.dbutil.basic.handler.WorkHandler;
import lombok.extern.log4j.Log4j;

import java.util.Date;
import java.util.concurrent.Exchanger;

/**
 * Description:Mysql任务处理类
 *
 * @JDK Version:1.8.0_40
 * Version: V1.0
 * Author: wangchun
 * Date: 2016/3/22 15:02
 */
@Log4j
public class DBWorkHandler implements WorkHandler {

    public String startTask(Object frontEndDBVo, ClientWorkCallback clientWorkCallback) throws Exception {
        ClientWorkManager clientWorkManager = ClientWorkManager.getInstance();
        FrontEndDBVo frontEndDBVoTemp = (FrontEndDBVo) frontEndDBVo;
        String workId = clientWorkManager.createClientWork(frontEndDBVoTemp, clientWorkCallback);

        //判断定时任务是否需要立即执行一次
        Integer executeImmediately = frontEndDBVoTemp.getExecuteImmediately();
        if (executeImmediately != null && executeImmediately.intValue() > 0) {
            clientWorkManager.schedule(workId,
                    new ClientTask() {
                        @Override
                        public void run() {
                            executeTask(getClientTaskCallback(), frontEndDBVoTemp, workId);
                        }
                    }
                    , 0);
        }

        //判断定时任务是否需要定时执行需求
        Date firstExecuteTime = frontEndDBVoTemp.getFirstExecuteTime();
        Long intervalTime = frontEndDBVoTemp.getIntervalTime();
        if (firstExecuteTime != null) {
            if (intervalTime != null && intervalTime.longValue() > 0) {
                //定时执行并循环执行任务
                clientWorkManager.scheduleAtFixedRate(workId,
                        new ClientTask() {
                            @Override
                            public void run() {
                                executeTask(getClientTaskCallback(), frontEndDBVoTemp, workId);
                            }
                        }
                        , firstExecuteTime, intervalTime);
            } else {
                //定时执行不循环执行任务
                clientWorkManager.schedule(workId,
                        new ClientTask() {
                            @Override
                            public void run() {
                                executeTask(getClientTaskCallback(), frontEndDBVoTemp, workId);
                            }
                        }
                        , firstExecuteTime);
            }
        }

        return workId;
    }

    private void executeTask(ClientTaskCallback clientTaskCallback, FrontEndDBVo frontEndDBVo, String workId) {
        try {
            DBDataQueryLoigic dbDataQueryLoigic = new DBDataQueryLoigic(frontEndDBVo);
            dbDataQueryLoigic.setDbDataQueryCallback(new DBDataQueryCallback() {
                @Override
                public void dataFinished(FrontEndDBVo frontEndDBVoTemp) {
                    clientTaskCallback.taskFinished(new TaskResultVo("", frontEndDBVo));
                    //关闭DatabaseSource
//                    try {
//                        dbDataQueryLoigic.closeDataSource();
//                    } catch (SQLException e) {
//                        e.printStackTrace();
//                    }
                }

                @Override
                public void dataPartial(FrontEndDBVo frontEndDBVoTemp) {
                    clientTaskCallback.taskPartialData(new TaskResultVo("", frontEndDBVoTemp));
                }

                @Override
                public void dataQueryException(Exception e, FrontEndDBVo frontEndDBVoTemp) {

                }
            });
            dbDataQueryLoigic.startQuery(workId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("DBWorkHandler executeTask,", e);
        }
    }

    public int pauseTask(String taskId) throws Exception {
        return 0;
    }

    public int resumeTask(String taskId) throws Exception {
        return 0;
    }

    public int stopTask(String taskId) throws Exception {
        return 0;
    }

    public void checkDirectory() {

    }

}
